package com.system.cacheCenterBackend.controller.redisDataController;

import cn.hutool.core.util.ObjectUtil;
import com.system.cacheCenterBackend.controller.hotKeyController.HotKeyController;
import com.system.cacheCenterBackend.service.StringCacheService;
import com.system.cacheClient.common.CacheR;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.CompletableFuture;

/**
 * String类型缓存控制器
 */
@RestController
@RequestMapping("/cache/string")
@RequiredArgsConstructor
public class StringCacheController {

    private final StringCacheService stringCacheService;

    /**
     * 设置字符串缓存值
     *
     * @param key   键
     * @param value 值
     * @return 统一返回结果
     */
    @PostMapping("/{key}")
    public CacheR<Boolean> set(@PathVariable String key, @RequestBody String value) {
        try {
            boolean result = stringCacheService.set(key, value);
            if (result) {
                // 添加键到热点数据控制器中
                HotKeyController.addKey("string", key);
                return CacheR.success(true, "设置缓存成功");
            } else {
                return CacheR.fail("设置缓存失败");
            }
        } catch (Exception e) {
            return CacheR.fail("设置缓存异常: " + e.getMessage());
        }
    }

    /**
     * 设置缓存值，带过期时间
     */
    @PostMapping("/set-with-timeout")
    public CacheR<Boolean> set(@RequestParam String key, @RequestParam String value, @RequestParam long timeout) {
        try {
            boolean result = stringCacheService.set(key, value, timeout);
            return CacheR.success(result);
        } catch (Exception e) {
            return CacheR.fail("设置缓存失败: " + e.getMessage());
        }
    }

    /**
     * 获取字符串缓存值
     *
     * @param key 键
     * @return 统一返回结果
     */
    @GetMapping("/{key}")
    public CacheR<String> get(@PathVariable String key) {
        try {
            String value = stringCacheService.get(key);
            if (value != null) {
                return CacheR.success(value, "获取缓存成功");
            } else {
                return CacheR.fail(404, "缓存不存在");
            }
        } catch (Exception e) {
            return CacheR.fail("获取缓存异常: " + e.getMessage());
        }
    }

    /**
     * 删除字符串缓存值
     *
     * @param key 键
     * @return 统一返回结果
     */
    @DeleteMapping("/{key}")
    public CacheR<Boolean> delete(@PathVariable String key) {
        try {
            boolean result = stringCacheService.delete(key);
            if (result) {
                // 从热点数据控制器中移除键
                HotKeyController.removeKey("string", key);
                return CacheR.success(true, "删除缓存成功");
            } else {
                return CacheR.fail("删除缓存失败");
            }
        } catch (Exception e) {
            return CacheR.fail("删除缓存异常: " + e.getMessage());
        }
    }

    /**
     * 检查缓存键是否存在
     */
    @GetMapping("/exists")
    public CacheR<Boolean> exists(@RequestParam String key) {
        try {
            boolean result = stringCacheService.exists(key);
            return CacheR.success(result);
        } catch (Exception e) {
            return CacheR.fail("检查缓存失败: " + e.getMessage());
        }
    }

    /**
     * 获取key的访问次数
     */
    @GetMapping("/access-count")
    public CacheR<Integer> getKeyAccessCount(@RequestParam String key) {
        try {
            int count = stringCacheService.getKeyAccessCount(key);
            return CacheR.success(count);
        } catch (Exception e) {
            return CacheR.fail("获取访问次数失败: " + e.getMessage());
        }
    }

    /**
     * 异步获取key的访问次数
     */
    @GetMapping("/access-count-async")
    public CacheR<Integer> getKeyAccessCountAsync(@RequestParam String key) {
        Integer keyAccessCountAsync = stringCacheService.getKeyAccessCountAsync(key);
        if (ObjectUtil.isEmpty(keyAccessCountAsync)){
            return CacheR.fail("获取访问次数失败");
        }
        return CacheR.success(keyAccessCountAsync);
    }

    /**
     * 检查key是否为热点key
     */
    @GetMapping("/is-hot-key")
    public CacheR<Boolean> isHotKey(@RequestParam String key) {
        try {
            boolean result = stringCacheService.isHotKey(key);
            return CacheR.success(result);
        } catch (Exception e) {
            return CacheR.fail("检查热点key失败: " + e.getMessage());
        }
    }

    /**
     * 重置key的访问统计
     */
    @PostMapping("/reset-statistics")
    public CacheR<Boolean> resetKeyStatistics(@RequestParam String key) {
        try {
            stringCacheService.resetKeyStatistics(key);
            return CacheR.success(true);
        } catch (Exception e) {
            return CacheR.fail("重置统计失败: " + e.getMessage());
        }
    }
}